package com.tmt.api.service;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.swak.annotation.GetMapping;
import com.swak.annotation.RestService;
import com.swak.entity.Result;
import com.tmt.api.entity.User;
import com.tmt.api.entity.UserAccount;

/**
 * 主从API测试
 * 
 * @author: lifeng
 * @Date: 2020/3/28 11:50
 */
@RestService(path = "/api/ms", value = "msApi")
public class MsApiService {

	@Autowired
	private UserService userService;

	/**
	 * 仅仅读
	 */
	@GetMapping("/get")
	public Result get() {
		User user = userService.get(1L);
		return Result.success(user);
	}

	/**
	 * 执行事务
	 */
	@GetMapping("/transactional")
	public Result transactional() {

		// 事务中注册
		UserAccount account = new UserAccount();
		account.setId(UUID.randomUUID().toString());
		account.setName("李锋测试");
		account.setHeadimg("没有图片");
		User user = userService.register(account);

		// 事务外查询
		return Result.success(userService.get(user.getId()));
	}

	/**
	 * 执行事务 -- 本身也是有事务
	 */
	@GetMapping("/transactional/self")
	@Transactional
	public Result transactionalSelf() {

		// 事务中注册
		this.transactional();

		// 事务中查询
		return this.get();
	}
}
